//
// Created by ta&to on 2022/8/25.
//

#ifndef LOGSSERVER_LOGS_FILE_H
#define LOGSSERVER_LOGS_FILE_H

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "logs_log.h"
#include "log.h"
#define logs_linefeed(p)          *p++ = LF;
#define LOGS_LINEFEED_SIZE       1
#define LOGS_LINEFEED            "\x0a"

#define logs_stdout               STDOUT_FILENO
#define logs_stderr               STDERR_FILENO
#define logs_set_stderr(fd)       dup2(fd, STDERR_FILENO)
#define logs_set_stderr_n         "dup2(STDERR_FILENO)"

enum path_type{
    absolutely,
    relative
};

struct file_s{
    void* name;
    void* fd;
    struct log_s *log;
    void *data;
};
typedef struct file_s file_n;

ssize_t write_fd(int console, void *buf, size_t n);
ssize_t write_file(file_n *file, void *buf, size_t size, off_t  offset);

char *pwd();
/**
 * 文件是否存在
 * @param filename
 * @return
 */
int isExist(char *filename);
int canR(char *filename);
int canW(char *filename);
int canX(char *filename);
/**
 * 判断是否是相对路径
 * @param path
 * @return 0、否 1、是
 */
isRelative(char *path);
/***
 * 将重复的字符归一
 * @param str
 * @param dot 需要归一的字符
 * @param ignore 忽略的字符
 * @param i_num  忽略次数
 * @return
 */
char* toOne(char *str, char dot, char *ignore, int i_num);
/**
 * 格式化文件地址
 * @param input
 * @return
 */
char* getAbsolutePath(char* input);
/**
 * 循环创建文件
 * @param path
 * @return
 */
int mkdirs(char *path);
/**
 * 以写的方式打开文件
 * @param path
 * @return
 */
FILE *wfile(char *file);
#define logs_write_console write_fd
#define logs_write_file write_file
#endif //LOGSSERVER_LOGS_FILE_H
